# -*- coding: UTF-8 -*-
from . import utilities
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator

#validate_screen_width() by author @ 2014
def validate_screen_width(max_width=70):
    """
    Check if the given string is shorter than max_width. Don't forget parentheses when used in models' fields.
    """
    def innerfn(string_to_test):
        if utilities.get_screen_width(string_to_test, max_width, False) >= max_width:
            raise ValidationError(u"最长不能超过{0}个半角字符.".format(max_width), code='invalid_string_width')
    return innerfn

#created by author @ 2014/6/25
def validate_permstr(input):
    return RegexValidator(
            r'^[01]{5}$',
            u'非法的版块权限.',
            'invalid_perm'
            )(input)

#validate_username() by author @ 2014/5/29
def validate_username(input):
    return RegexValidator(
               r'^[a-zA-Z](_?[a-zA-Z0-9]+)*$',
               u'用户名需以字母开头,字母或数字结尾,不能含有连续下划线.',
               'invalid_username'
               )(input)

#validate_boardname() by author @ 2014/5/29
def validate_boardname(input):
    return RegexValidator(
               r'^[a-zA-Z][a-zA-Z0-9]{2,19}$',
               u'板块英文名只能由字母和数字组成,须以字母开头,长度限制在3-20个字符.',
               'invalid_boardname'
               )(input)

#validate_showname() by author @ 2014/5/29
def validate_showname(input):
    return RegexValidator(
               r'^[^\s~`,\.\[\]{}\(\)<>\?/\\\+\-_\*&\^%\$#@!\:;""''|]*$',
               u'板块名不能包含标点符号和特殊字符.',
               'invalid_showname'
               )(input)

#created by author @ 2014/6/12
def validate_latinchar(input):
    return RegexValidator(
               r'^[a-zA-Z0-9]+$',
               u'信息代号只能由字母和数字组成.',
               'invalid_showname'
               )(input)
